using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
namespace MemoryGame
{
    public class StateGameplay : DEF
    {
        public static Texture2D backgroundImageSelectLevel;
        public static Texture2D backgroundImage;
       // public static Texture2D[] texttureCard = new Texture2D[16];
        public static Texture2D[] texttureEffect = new Texture2D[5];        
        public static bool isIngame = false;
        public static long timepause;
        static Map map;
        public static void SendMessage(int type)
        {

            switch (type)
            {
                case MESSAGE_CTOR:
                    //image/character/character_1.png
                    isIngame = true;

                    //if (backgroundImage != null)
                    //    backgroundImage.Dispose();
                    //backgroundImage = null;
                   // Random a = new Random();
                  //  int r = a.Next(5) + 1;
                  //  a = null;
                  //  backgroundImage = Game1.contentManager.Load<Texture2D>("image//screen//screen" + r);
                    if(backgroundImage == null)
                        backgroundImage = Game1.contentManager.Load<Texture2D>("image//screen//screen1");
                    int j;
                   
                   
                    for (int i = 0; i < 5; i++)
                    {
                        if (texttureEffect[i] == null)
                            texttureEffect[i] = Game1.contentManager.Load<Texture2D>("image//effect//effect" + i);
                    }
                    

                    map = new Map();
                    switch (Map.gameDifficultMode)
                    {
                        case Map.GAME_MODE_EASY://easy
                            Map.Init(Map.GAME_EASY_COL, Map.GAME_EASY_ROW);
                            break;
                        case Map.GAME_MODE_NORMAL://normal
                            Map.Init(Map.GAME_NORMAL_COL, Map.GAME_NORMAL_ROW);
                            break;
                        case Map.GAME_MODE_HARD://hard
                            Map.Init(Map.GAME_HARD_COL, Map.GAME_HARD_ROW);
                            break;
                        case Map.GAME_MODE_EXTREME://extreme
                            Map.Init(Map.GAME_EXTREME_COL, Map.GAME_EXTREME_ROW);
                            break;
                    }
                   
                    // Map.NewGame();
                    DEF.LABEL_SCORE_Y = DEF.LABEL_LEVEL_Y + (int)(Game1.fontNormal.MeasureString("Score: 660").Y);
                    DEF.LABEL_SCORE_X = SCREEN_WIDTH - (int)(Game1.fontNormal.MeasureString("Score: 660").X);

                    DEF.BUTTON_TIMER_BAR_W = (int)(DEF.texttureButtonMenu[DEF.FRAME_TIMER_BAR_0].Width * Game1.SCALE_X);
                    DEF.BUTTON_TIMER_BAR_H = (int)(DEF.texttureButtonMenu[DEF.FRAME_TIMER_BAR_0].Height * Game1.SCALE_Y);
                    DEF.BUTTON_TIMER_BAR_X = (int)(Game1.fontNormal.MeasureString("Level : 60").X);//fontsmall_Yellow.stringWidth("Level : 60") + DEF.DIALOG_BUTTON_CONFIRM_W / 2;
                    DEF.BUTTON_TIMER_BAR_Y = 2;

                    DEF.BUTTON_IGM_W = (int)(DEF.texttureButtonMenu[DEF.FRAME_PAUSE_NORMAL].Width * Game1.SCALE_X);
                    DEF.BUTTON_IGM_H = (int)(DEF.texttureButtonMenu[DEF.FRAME_PAUSE_NORMAL].Height * Game1.SCALE_Y);
                    DEF.BUTTON_CHANGE_TILE_W = DEF.BUTTON_HINT_W = (int)(DEF.texttureButtonMenu[DEF.FRAME_BUTTON_CUSTOM_HINT].Width * Game1.SCALE_X); ;
                    DEF.BUTTON_CHANGE_TILE_H = DEF.BUTTON_HINT_H = (int)(DEF.texttureButtonMenu[DEF.FRAME_BUTTON_CUSTOM_HINT].Height * Game1.SCALE_X); ;
                    DEF.BUTTON_IGM_X = SCREEN_WIDTH - DEF.BUTTON_IGM_W;
                    DEF.BUTTON_IGM_Y =2;
                   

                    //SoundManager.stopSound(SoundManager.SOUND_TITLE);
                    //SoundManager.mMediaPlayer
                    SoundManager.playsoundLoop(1, true);
                    //clean wall in map
                    //GameLayer.arrayListDataMap.get(0)[6][6] = -1;
                    //GameLayer.arrayListDataMap.get(0)[7][6] = -1;

                    //Log.d("map.doorExit_Row"," " + map.doorExit_Row);
                    //Log.d("map.doorExit_Col"," " + map.doorExit_Col);

                    break;
                case MESSAGE_UPDATE:
                    //cheat
                    //  if (isKeyReleased(KeyEvent.KEYCODE_9))
                    //  {
                    //     StateWinLose.isWin = true;
                    //     ChristmasActivity.changeState(IConstant.STATE_WINLOSE);
                    // }
                    //end cheat
                    if (Dialog.isShowDialog)
                        return;
                    updatetouch();
                    Map.Update();

                    break;

                case MESSAGE_PAINT:
                    Game1.spriteBatch.Draw(backgroundImage, new Vector2(0, 0), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);


                    if (Dialog.isShowDialog)
                        return;
                    if (Game1.mCurrentState != Game1.STATE_GAMEPLAY)
                        return;
                    Map.DrawGame(Game1.spriteBatch);
                    drawHUD(Game1.spriteBatch);
                    break;
                case MESSAGE_DTOR:
                    isIngame = false;
                    break;
            }
        }

        public static void drawHUD(SpriteBatch c)
        {
            //score
            //spriteDPad.drawAFrame(PikachuActivity.mainCanvas, DEF.FRAME_SCORE, 0, 0);
            //mainPaint.setStyle(Style.FILL);
            //mainPaint.setColor(0xFF2b8fbe);
            //	c.drawRect(0, 0, SCREEN_WIDTH, DEF.BUTTON_TIMER_BAR_H, mainPaint);            




            // if (Dialog.isShowDialog || mCurrentState != STATE_GAMEPLAY)
            // return;
            // draw pause button
            if (InputState.isTouchDrapInRect(DEF.BUTTON_IGM_X, DEF.BUTTON_IGM_Y, DEF.BUTTON_IGM_W, DEF.BUTTON_IGM_H))
                Game1.spriteBatch.Draw(texttureButtonMenu[DEF.FRAME_PAUSE_HIGHTLIGHT], new Vector2(DEF.BUTTON_IGM_X, DEF.BUTTON_IGM_Y), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);
            else
                Game1.spriteBatch.Draw(texttureButtonMenu[DEF.FRAME_PAUSE_NORMAL], new Vector2(DEF.BUTTON_IGM_X, DEF.BUTTON_IGM_Y), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);

         //   Game1.spriteBatch.DrawString(Game1.fontNormal, "Timer: " + Map.totalTimePlayGame / 1000, new Vector2(360*Game1.SCALE_X, DEF.LABEL_LEVEL_Y), Color.Blue);

            Game1.spriteBatch.DrawString(Game1.fontNormal, "Level:" + (Game1.mcurrentlevel + 1), new Vector2(SCREEN_WIDTH - DEF.BUTTON_IGM_W - (int)Game1.fontNormal.MeasureString("Level:19").X, 2/*DEF.LABEL_SCORE_Y*/), Color.Yellow);


            // timer bar
            // spriteDPad.drawAFrame(TapFruit.mainCanvas, DEF.FRAME_TIMER_BAR_0,
            // DEF.BUTTON_TIMER_BAR_X, DEF.BUTTON_TIMER_BAR_Y);

            // double width =
            // StateGameplay.spriteDPad.getFrameWidth(DEF.FRAME_TIMER_BAR_1);
            // width = width - width * (Map.mTimerCount * 1.0) / Map.mTimerDecrease;

            // TapFruit.mainCanvas.save();
            // TapFruit.mainCanvas.clipRect(DEF.BUTTON_TIMER_BAR_X,
            // DEF.BUTTON_TIMER_BAR_Y, DEF.BUTTON_TIMER_BAR_X + (int) width,
            // DEF.BUTTON_TIMER_BAR_Y + 32);
            // spriteDPad.drawAFrame(TapFruit.mainCanvas, DEF.FRAME_TIMER_BAR_1,
            // DEF.BUTTON_TIMER_BAR_X, DEF.BUTTON_TIMER_BAR_Y);
            // TapFruit.mainCanvas.restore();

        }

        public static void updatetouch()
        {
            if (InputState.isBackKeyPress() || InputState.isTouchReleaseInRect(DEF.BUTTON_IGM_X, DEF.BUTTON_IGM_Y, DEF.BUTTON_IGM_W, DEF.BUTTON_IGM_H))
            {
                SoundManager.playSound(SoundManager.SOUND_SELECT);
                Game1.changeState(Game1.STATE_IGM, true, false);
            }

            //  if (InputState.isTouchReleaseInRect(DEF.BUTTON_HINT_X, DEF.BUTTON_HINT_Y, DEF.BUTTON_HINT_W, DEF.BUTTON_HINT_H))
            //  {
            //      if (Map.countHint > 0)
            //      {
            //         map.searchPair();
            //          Map.countHint--;
            //    }
            // }

            // if (InputState.isTouchReleaseInRect(DEF.BUTTON_CHANGE_TILE_X, DEF.BUTTON_CHANGE_TILE_Y, DEF.BUTTON_CHANGE_TILE_W, DEF.BUTTON_CHANGE_TILE_H))
            // {
            //    if (Map.countAutoSort > 0)
            //    {
            //      map.autoSortMap();
            //       Map.countAutoSort--;
            //  }
        }
    }
}

